<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="text" label="问题标题" required>
				<uni-easyinput placeholder="" v-model="formData.text" />
			</uni-forms-item>
			<!-- <uni-forms-item name="parent_id" label="">
  <uni-easyinput v-model="formData.parent_id" />
</uni-forms-item> -->
			<uni-forms-item name="is_radio" label="是否单选" required>
				<switch @change="binddata('is_radio', $event.detail.value)" :checked="formData.is_radio" />
			</uni-forms-item>
			<uni-forms-item name="is_other" label="是否接受输入其他答案">
				<switch @change="binddata('is_other', $event.detail.value)" :checked="formData.is_other" />
			</uni-forms-item>
			<uni-forms-item name="is_required" label="是否必填">
				<switch @change="binddata('is_required', $event.detail.value)" :checked="formData.is_required" />
			</uni-forms-item>
			<uni-forms-item name="anonymous" label="是否允许匿名回答">
				<switch @change="binddata('anonymous', $event.detail.value)" :checked="formData.anonymous" />
			</uni-forms-item>
			<uni-forms-item name="deadline" label="截止时间">
				<uni-datetime-picker :timestamp="true" :value="formData.deadline" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/opendb-vote-question.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'opendb-vote-question';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"text": "",
					"parent_id": "",
					"is_radio": true,
					"is_other": null,
					"is_required":null,
					"anonymous": null,
					"deadline": null
				},
				formOptions: {},
				rules: {
					...getValidator(["text", "parent_id", "is_radio", "is_other", "is_required", "anonymous", "deadline"])
				}
			}
		},
		onLoad(e) {
			const id = e.id
			this.formDataId = id
			this.getDetail(id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 uni-clientDB 提交数据
				db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field('text,parent_id,is_radio,is_other,anonymous,deadline').get().then((
					res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
